<?php
/**
 * Minhlog Minh_Resource 路由资源类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Resource
 * @copyright  Copyright (c) 2009-2010 Minh.Shu Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Router.php 37 2010-05-10 16:20:47Z Minh.Shu $
 *
 */
class Minh_Resource_Router extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * 路由资源
     * @access private
     * @var object
     */
    private $_router;

    /**
     * 初始化方法
     * @return void
     */
    public function init ()
    {
        return $this->_getRouter();
    }

    /**
     * 获取视图资源对象
     * @access private
     * @return $view
     */
    private function _getRouter ()
    {
        if (null === $this->_router) {
            $router = Zend_Controller_Front::getInstance()->getRouter();
            $options = $this->getOptions();
            //添加静态路由
            foreach ($options['static'] as $key => $static) {
                $route = new Zend_Controller_Router_Route_Static($static['routerule'],
                array('controller' => $static['controller'], 'action' => $static['action'], 'module' => $static['module']));
                $router->addRoute($key, $route);
            }
            //添加正则路由
            foreach ($options['regex'] as $key => $regex) {
                $route = new Zend_Controller_Router_Route_Regex($regex['routerule'], $regex['urlrule'], $regex['paramrule'], $regex['regexrule']);
                $router->addRoute($key, $route);
            }
            $this->_router = $router;
        }
        return $this->_router;
    }
}